﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DotNetNuke.Entities.Tabs;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Security.Permissions;
using DotNetNuke.Entities.Content;
using System.Text;
using System.Xml;

namespace iLangl.Customer.DNN
{
	public class PermissionManager
	{
		public static String ExportModulePermissions(ModulePermissionCollection permissions)
		{
			StringBuilder sb = new StringBuilder();
			sb.Append("<ModulePermissions>");
			string elementMask= "<{0} value='{1}' />";
			foreach (ModulePermissionInfo mpi in permissions)
			{
				sb.Append("<ModulePermission>");
					sb.AppendFormat(elementMask, "AllowAccess", mpi.AllowAccess);
					sb.AppendFormat(elementMask, "CreatedByUserID", mpi.CreatedByUserID);
					sb.AppendFormat(elementMask, "CreatedOnDate", mpi.CreatedOnDate);
					sb.AppendFormat(elementMask, "DisplayName", mpi.DisplayName);
					sb.AppendFormat(elementMask, "KeyID", mpi.KeyID);
					sb.AppendFormat(elementMask, "LastModifiedByUserID", mpi.LastModifiedByUserID);
					sb.AppendFormat(elementMask, "LastModifiedOnDate", mpi.LastModifiedOnDate);
					sb.AppendFormat(elementMask, "ModuleDefID", mpi.ModuleDefID);
					sb.AppendFormat(elementMask, "ModuleID", mpi.ModuleID);
					sb.AppendFormat(elementMask, "ModulePermissionID", mpi.ModulePermissionID);
					sb.AppendFormat(elementMask, "PermissionCode", mpi.PermissionCode);
					sb.AppendFormat(elementMask, "PermissionID", mpi.PermissionID);
					sb.AppendFormat(elementMask, "PermissionKey", mpi.PermissionKey);
					sb.AppendFormat(elementMask, "PermissionName", mpi.PermissionName);
					sb.AppendFormat(elementMask, "RoleID", mpi.RoleID);
					sb.AppendFormat(elementMask, "RoleName", mpi.RoleName);
					sb.AppendFormat(elementMask, "UserID", mpi.UserID);
					sb.AppendFormat(elementMask, "Username", mpi.Username);
				sb.Append("</ModulePermission>");

			}
			sb.Append("</ModulePermissions>");
			return sb.ToString();
		}

		public static String ExportTabPermissions(TabPermissionCollection permissions)
		{
			StringBuilder sb = new StringBuilder();
			sb.Append("<TabPermissions>");
			string elementMask = "<{0} value='{1}' />";
			foreach (TabPermissionInfo mpi in permissions)
			{
				sb.Append("<TabPermission>");
				sb.AppendFormat(elementMask, "AllowAccess", mpi.AllowAccess);
				sb.AppendFormat(elementMask, "CreatedByUserID", mpi.CreatedByUserID);
				sb.AppendFormat(elementMask, "CreatedOnDate", mpi.CreatedOnDate);
				sb.AppendFormat(elementMask, "DisplayName", mpi.DisplayName);
				sb.AppendFormat(elementMask, "KeyID", mpi.KeyID);
				sb.AppendFormat(elementMask, "LastModifiedByUserID", mpi.LastModifiedByUserID);
				sb.AppendFormat(elementMask, "LastModifiedOnDate", mpi.LastModifiedOnDate);
				sb.AppendFormat(elementMask, "ModuleDefID", mpi.ModuleDefID);
				sb.AppendFormat(elementMask, "PermissionCode", mpi.PermissionCode);
				sb.AppendFormat(elementMask, "PermissionID", mpi.PermissionID);
				sb.AppendFormat(elementMask, "PermissionKey", mpi.PermissionKey);
				sb.AppendFormat(elementMask, "PermissionName", mpi.PermissionName);
				sb.AppendFormat(elementMask, "RoleID", mpi.RoleID);
				sb.AppendFormat(elementMask, "RoleName", mpi.RoleName);
				sb.AppendFormat(elementMask, "TabID", mpi.TabID);
				sb.AppendFormat(elementMask, "TabPermissionID", mpi.TabPermissionID);
				sb.AppendFormat(elementMask, "UserID", mpi.UserID);
				sb.AppendFormat(elementMask, "Username", mpi.Username);
				sb.Append("</TabPermission>");

			}
			sb.Append("</TabPermissions>");
			return sb.ToString();
		}

		public static TabPermissionCollection ImportTabPermissions(String serializedPermissions)
		{
			XmlDocument document = new XmlDocument();

			TabPermissionCollection result = new TabPermissionCollection();

			XmlNodeList list = document.SelectNodes("//ModulePermission");
			foreach (XmlNode node in list)
			{
				TabPermissionInfo mpi = new TabPermissionInfo();
				mpi.AllowAccess = bool.Parse(node.SelectSingleNode("//AllowAccess").Value);
				mpi.DisplayName = node.SelectSingleNode("//DisplayName").Value;
				mpi.KeyID = int.Parse(node.SelectSingleNode("//KeyID").Value);
				mpi.ModuleDefID = int.Parse(node.SelectSingleNode("//ModuleDefID").Value);
				mpi.PermissionCode = node.SelectSingleNode("//PermissionCode").Value;
				mpi.PermissionID = int.Parse(node.SelectSingleNode("//PermissionID").Value);
				mpi.PermissionKey = node.SelectSingleNode("//PermissionKey").Value;
				mpi.PermissionName = node.SelectSingleNode("//PermissionName").Value;
				mpi.RoleID = int.Parse(node.SelectSingleNode("//RoleID").Value);
				mpi.RoleName = node.SelectSingleNode("//RoleName").Value;
				mpi.TabID = int.Parse(node.SelectSingleNode("//TabID").Value);
				mpi.TabPermissionID = int.Parse(node.SelectSingleNode("//TabPermissionID").Value);
				mpi.UserID = int.Parse(node.SelectSingleNode("//UserID").Value);
				mpi.Username = node.SelectSingleNode("//Username").Value;
				result.Add(mpi);
			}

			return result;
		}

		public static ModulePermissionCollection ImportModulePermissions(String serializedPermissions)
		{
			XmlDocument document = new XmlDocument();
			
			ModulePermissionCollection result = new ModulePermissionCollection();

			XmlNodeList list = document.SelectNodes("//ModulePermission");
			foreach (XmlNode node in list)
			{
				ModulePermissionInfo mpi = new ModulePermissionInfo();
				mpi.AllowAccess = bool.Parse(node.SelectSingleNode("//AllowAccess").Value);
				mpi.DisplayName = node.SelectSingleNode("//DisplayName").Value;
				mpi.KeyID = int.Parse(node.SelectSingleNode("//KeyID").Value);
				mpi.ModuleDefID = int.Parse(node.SelectSingleNode("//ModuleDefID").Value);
				mpi.ModuleID = int.Parse(node.SelectSingleNode("//ModuleID").Value);
				mpi.ModulePermissionID = int.Parse(node.SelectSingleNode("//ModulePermissionID").Value);
				mpi.PermissionCode = node.SelectSingleNode("//PermissionCode").Value;
				mpi.PermissionID = int.Parse(node.SelectSingleNode("//PermissionID").Value);
				mpi.PermissionKey = node.SelectSingleNode("//PermissionKey").Value;
				mpi.PermissionName = node.SelectSingleNode("//PermissionName").Value;
				mpi.RoleID = int.Parse(node.SelectSingleNode("//RoleID").Value);
				mpi.RoleName = node.SelectSingleNode("//RoleName").Value;
				mpi.UserID = int.Parse(node.SelectSingleNode("//UserID").Value);
				mpi.Username = node.SelectSingleNode("//Username").Value;
				result.Add(mpi);
			}

			return result;
		}

	}
}